from django.shortcuts import render, HttpResponse, reverse, redirect
from django.views.generic import View, ListView
from django.utils.decorators import method_decorator
from .models import Article


class Index(View):
    def get(self, request, *args, **kwargs):
        articles = []
        for i in range(1, 103):
            article = Article(title='标题%s' % i, content='内容%s' % i)
            articles.append(article)
        Article.objects.bulk_create(articles)
        return HttpResponse('success')


class ArtcileList(ListView):
    # 1.model：重写model类属性，指定这个列表是给哪个模型的
    model = Article
    # 2.template_name：指定这个列表的模板
    template_name = 'article_list_1.html'
    # 3.paginate_by：指定这个列表一页中展示多少条数据
    paginate_by = 10
    # 4.context_object_name：指定这个列表模型在模板中的参数名称
    context_object_name = 'articles'
    # 5.ordering：指定这个列表的排序方式
    ordering = 'create_time'
    # 6.page_kwarg：获取第几页的数据的参数名称。默认是page
    page_kwarg = 'page'

    # 7.get_context_data：获取上下文的数据
    def get_context_data(self, **kwargs):
        context = super(ArtcileList, self).get_context_data(**kwargs)
        # # 1.Paginator常用属性和方法
        paginator = context.get('paginator')
        # # 2.page常用属性和方法
        # print('page..............')
        page_obj = context.get('page_obj')
        result = self.get_pagination_data(paginator, page_obj)
        context.update(result)
        return context

    # 定义通用分页器算法
    def get_pagination_data(self, paginator, page_obj, around_count=2):
        """
        :param paginator:
        :param page_obj:
        :param around_count: 左右间隔页数
        :return:
        """
        current_page = page_obj.number  # TODO: 当前页
        total_page = paginator.num_pages  # TODO: 总页数
        has_left_point = False  # TODO: 是否左边显示...
        has_right_point = False  # TODO: 是否右边边显示...
        # TODO: 计算左侧页码
        if current_page - around_count > 1:  # TODO: 当前页 - around_count > 0，确定range start
            left_pages = range(current_page - around_count, current_page)
            has_left_point = True
        else:
            left_pages = range(1, current_page)
        # TODO: 计算右侧页码
        if total_page - current_page > around_count:  # TODO: 当前页 <= 总页数 - around_count， 确定stop不超过总页数
            has_right_point = True
            right_pages = range(current_page + 1, current_page + around_count + 1)
        else:
            right_pages = range(current_page + 1, total_page + 1)
        return {
            'current_page': current_page,
            'total_page': total_page,
            'left_pages': left_pages,
            'right_pages': right_pages,
            'has_left_point': has_left_point,
            'has_right_point': has_right_point
        }

    # 8.如果你提取数据的时候，并不是要把所有数据都返回，那么你可以重写这个方法.
    # 将一些不需要展示的数据给过滤掉。
    # def get_queryset(self):
    #     return Article.objects.filter(id__lte=9)


def login_required(func):
    def wrapper(request, *args, **kwargs):
        username = request.GET.get('username')
        if username:
            return func(request, *args, **kwargs)
        else:
            return redirect(reverse('login'))

    return wrapper


class LoginView(View):
    def get(self, request, *args, **kwargs):
        return HttpResponse('登录')


# TODO: 给类视图添加装饰器
# 2.直接装饰在整个类上
@method_decorator([login_required], name='dispatch')
class ProfileView(View):
    def get(self, request, *args, **kwargs):
        username = request.GET.get('username')
        response = HttpResponse(content='当前登录用户：%s' % username)
        return response

    # 1.装饰dispatch方法
    # @method_decorator(login_required)
    # def dispatch(self, request, *args, **kwargs):
    #     return super(ProfileView, self).dispatch(request, *args, **kwargs)
